package app.web.cartoon.analyse.fans;

import java.util.HashMap;
import java.util.Map;

import app.web.spe.CartoonSPE;
import cn.lily.com.component.schema.table.multi.TMultiCell;
import cn.lily.com.component.schema.table.multi.item.TMultiBody;
import cn.lily.com.component.schema.table.multi.item.TMultiTitle;
import cn.lily.com.db.DBFactory;
import cn.lily.com.db.container.reader.DataTable;
import cn.lily.com.exception.YaconException;
import cn.lily.com.lang.Basic;
import cn.lily.web.page.BusinessPage;
import cn.lily.web.widget.expand.grid.TMultiGrid;
import cn.lily.web.widget.standard.TButton;
import cn.lily.web.widget.standard.TButtonPilot;
/**
 * 动漫番数分析页面
 * @author JiaJ
 * @date 2017年12月3日
 *
 */
public class CartoonFans extends BusinessPage {
	
	/**
	 * 数据集合
	 */
	public Map<String,String> dataMap = new HashMap<String,String>();

	@Override
	public void page_onLoad() throws YaconException {
		
		TMultiGrid cartoonFanGrid = new TMultiGrid("cartoonFanGrid");
		
		this.instData();
		
		this.instGrid(cartoonFanGrid);
		
		this.instData(cartoonFanGrid);
		
		this.getPage().addElement(cartoonFanGrid);
			
	}
	
	/**
	 * 初始化数据
	 */
	public void instData() {
		
		this.dataMap.clear();
		
		//番数统计
		String sqlFan = "select CAN_FANS,count(*) as CAN_NUM from ${schema}.LEO_CAN_TIME group by CAN_FANS";	
		DataTable dt = DBFactory.getActionDB().executeQueryTable(sqlFan);
		while(dt.next()){
			String fans = dt.getString("CAN_FANS");
			String num = dt.getString("CAN_NUM");
			this.dataMap.put(fans, num);
		}
		
		//年数统计
		String sqlYear = "select CAN_YEAR,count(*) as CAN_NUM from ${schema}.LEO_CAN_TIME group by CAN_YEAR";
		DataTable yt = DBFactory.getActionDB().executeQueryTable(sqlYear);
		while(yt.next()){
			String year = yt.getString("CAN_YEAR");
			String num = yt.getString("CAN_NUM");
			this.dataMap.put(year, num);
		}
		
	}
	
	/**
	 * 初始化标题
	 * @param cartoonFanGrid
	 */
	public void instGrid(TMultiGrid cartoonFanGrid){
				
		cartoonFanGrid.createView("cartoonYear", "动漫年份信息").getCell().setFontColor("#000000").setBackColor("#FFFFFF");
		cartoonFanGrid.createView("cartoonJan", "1月番").getCell().setFontColor("#000000").setBackColor("#FFFFFF");
		cartoonFanGrid.createView("cartoonApr", "4月番").getCell().setFontColor("#000000").setBackColor("#FFFFFF");
		cartoonFanGrid.createView("cartoonJul", "7月番").getCell().setFontColor("#000000").setBackColor("#FFFFFF");
		cartoonFanGrid.createView("cartoonOct", "10月番").getCell().setFontColor("#000000").setBackColor("#FFFFFF");
			
		/* 设置标题 */
		TButtonPilot cartoonFanPilot = ButtonPilot("cartoonFanPilot");
		cartoonFanPilot.setPilotIco("/widget/buttonPilot.gif");
		cartoonFanPilot.setPilotText("动漫番数信息");
		cartoonFanPilot.addButtons(new TButton("refresh"));
		
		cartoonFanGrid.setButtonPilot(cartoonFanPilot);
		
		//设置双击事件
		cartoonFanGrid.setDataDbClick("cartoonFanGrid_dbclick");
		
	}
	
	/**
	 * 数据加载
	 * @param testGrid
	 */
	public void instData(TMultiGrid testGrid){

		TMultiBody body = testGrid.getBody();
		
		int rowIdx = 0;
		
		int startYear = CartoonSPE.MAX_YEAR;
		int endYear = 2000;
		
		for(;startYear >= endYear;startYear--){
			
			TMultiCell cell1 = body.createDataCell(rowIdx, 0).setFieldValue(startYear+"年"+"-"+this.getNum(startYear+""));			
			this.setColor(cell1, this.getNum(startYear+""));
			
			TMultiCell cell2 = body.createDataCell(rowIdx, 1).setFieldValue(startYear+"年1月"+"-"+this.getNum(startYear+"01"));			
			this.setColor(cell2, this.getNum(startYear+"01"));
			
			TMultiCell cell3 = body.createDataCell(rowIdx, 2).setFieldValue(startYear+"年4月"+"-"+this.getNum(startYear+"04"));			
			this.setColor(cell3, this.getNum(startYear+"04"));
			
			TMultiCell cell4 = body.createDataCell(rowIdx, 3).setFieldValue(startYear+"年7月"+"-"+this.getNum(startYear+"07"));			
			this.setColor(cell4, this.getNum(startYear+"07"));
			
			TMultiCell cell5 = body.createDataCell(rowIdx, 4).setFieldValue(startYear+"年10月"+"-"+this.getNum(startYear+"10"));			
			this.setColor(cell5, this.getNum(startYear+"10"));
			
			rowIdx++;		
			
		}
		
		int startYearEx = 1999;
		int endYearEx = CartoonSPE.MIN_YEAR;
		
		int row = (startYearEx - endYearEx) / 5 + ((startYearEx - endYearEx)%5==0?0:1);
		
		for(int i=0;i<row;i++){
			
			for(int j=0;j<5;j++){	
				
				int Num = this.getNum(startYearEx+"");
				
				TMultiCell activeCell = body.createDataCell(rowIdx, j).setFieldValue(startYearEx+"年"+"-"+Num);			
				this.setColor(activeCell, Num);
				
				startYearEx--;
				if(startYearEx<endYearEx){
					break;
				}
			}
			
			rowIdx++;
		}
		
	}
	
	public TMultiCell setColor(TMultiCell cell,int Num){
		
		//绿色
		if(Num == 0){
			cell.setBackColor("#EAEAEA").setFontColor("#000000");
		}
		
		//中绿
		else if (Num > 0 && Num <= 10){
			cell.setBackColor("#9AFF9A").setFontColor("#000000");
		}
		
		//深绿
		else if (Num > 10 && Num <= 20){
			cell.setBackColor("#90EE90").setFontColor("#000000");
		}
		
		//蓝色
		else if (Num > 20 && Num <= 30){
			cell.setBackColor("#BBFFFF").setFontColor("#000000");
		}
		
		//蓝色
		else if (Num > 30 && Num <= 40){
			cell.setBackColor("#98F5FF").setFontColor("#000000");
		}
		
		//蓝色
		else if (Num > 40 && Num <= 50){
			cell.setBackColor("#87CEFF").setFontColor("#000000");
		}
		
		//黄色
		else if (Num > 50 && Num <= 60){
			cell.setBackColor("#FFF68F").setFontColor("#000000");
		}
		
		//黄色
		else if (Num > 60 && Num <= 70){
			cell.setBackColor("#EEEE00").setFontColor("#000000");
		}
		
		//橙色
		else if (Num > 70 && Num <= 80){
			cell.setBackColor("#FFDEAD").setFontColor("#000000");
		}
		
		//橙色
		else if (Num > 80 && Num <= 90){
			cell.setBackColor("#FFB90F").setFontColor("#000000");
		}
		
		//粉色
		else if (Num > 90 && Num <= 100){
			cell.setBackColor("#FFE1FF").setFontColor("#000000");
		}
		
		//粉色
		else if (Num > 100 && Num <= 110){
			cell.setBackColor("#FFBBFF").setFontColor("#000000");
		}
		
		//粉色
		else if (Num > 110 && Num <= 120){
			cell.setBackColor("#FF83FA").setFontColor("#000000");
		}
		
		//红色
		else if (Num > 120 && Num <= 130){
			cell.setBackColor("#FF6A6A").setFontColor("#000000");
		}
		
		//红色
		else if (Num > 130 && Num <= 150){
			cell.setBackColor("#FF00FF").setFontColor("#000000");
		}
		
		//紫色
		else {
			cell.setBackColor("#FF0000").setFontColor("#000000");
		}
		
		return cell;
		
		
	}

	/**
	 * null转换为空字符串
	 * @param str
	 * @return
	 */
	public String getString(String str){
		if(str == null){
			return "";
		}
		return str;
	}
	
	public int getNum(String fanID){
		String retNum = this.dataMap.get(fanID);
		if(retNum == null){
			retNum = "0";
		}
		return Basic.getInt(retNum);
	}

	public void refresh() throws YaconException {
		
		TMultiGrid cartoonFanGrid = new TMultiGrid("cartoonFanGrid");
		
		this.instData();
		
		this.instGrid(cartoonFanGrid);
		
		this.instData(cartoonFanGrid);
			
		this.setSelfMessage(cartoonFanGrid.getDataElement());
		
	}

}
